/*
ID: saboia
LANG: C++
TASK: pageant
*/
//Accepted
#include <vector>
#include <set>
#include <algorithm>
#include <string>
#include <cmath>
#include <queue>
#include <map>
#include <iostream>
#include <list>
#include <deque>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;



int main (){
	freopen("pageant.in", "r", stdin);freopen("pageant.out", "w", stdout);
	//freopen("in.txt", "r", stdin);
	int n, m, i, j, d[4][2]={{0,1},{1,0},{0,-1},{-1,0}}, ii, jj, k, w;
	char a[50][51], ok;
	bool pode[50][50];
	
	memset(pode,1,sizeof(pode));
	
	scanf("%d%d", &n, &m);
	
	for(i=0;i<n;++i){
		scanf("%s", a[i]);
	}
	queue<int> x, y;
	
	ok=1;
	for(i=0;i<n&&ok;++i){
		for(j=0;j<m&&ok;++j){
			ok=(a[i][j]=='.');
		}
	}
	x.push(i-1);x.push(j-1);
	pode[i-1][j-1]=0;
	while(!x.empty()){
		i=x.front();x.pop();
		j=x.front();x.pop();
		ok=0;
		for(k=0;k<4;++k){
			ii=i+d[k][0];
			jj=j+d[k][1];
			if((0<=ii&&ii<n)&&(0<=jj&&jj<m)){
				if(a[ii][jj]=='.'){
					ok=1;
				}
				else if(pode[ii][jj]){
					pode[ii][jj]=0;
					x.push(ii);
					x.push(jj);
				}
			}
		}
		if(ok){
			y.push(i);
			y.push(j);
			y.push(0);
		}
	}
	while(!y.empty()){
		i=y.front();y.pop();
		j=y.front();y.pop();
		w=y.front();y.pop();
		for(k=0;k<4;++k){
			ii=i+d[k][0];
			jj=j+d[k][1];
			if((0<=ii&&ii<n)&&(0<=jj&&jj<m)){
				if(pode[ii][jj]){
					pode[ii][jj]=0;
					
					if(a[ii][jj]!='.')goto achou;
					else{
						y.push(ii);
						y.push(jj);
						y.push(w+1);
					}
				}
			}
		}
	}
	achou: ;
	printf("%d\n", w);
	
	return 0;
}
